package com.firword.common.controller;

import com.firword.common.constant.SysDicTypeEnum;
import com.firword.common.domain.SysDic;
import com.firword.common.domain.base.Combobox;
import com.firword.common.domain.base.Pager;
import com.firword.common.domain.base.QueryResult;
import com.firword.common.service.SysDicManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: jingyajun
 * @date: 2020/9/27
 * @description:
 * @since:
 */
@Controller
@RequestMapping(value = "sysDic")
public class SysDicController {
    @Resource(name = "SysDicManager")
    private SysDicManager sysDicManager;

    @GetMapping(value = "comboboxList.json")
    @ResponseBody
    public List<Combobox> comboboxList() {
        return SysDicTypeEnum.comboboxList();
    }

    @RequestMapping(value = "updateSysDic.json")
    @ResponseBody
    public ModelMap updateSysDic(ModelMap modelMap, @RequestBody SysDic sysDic) {
        sysDicManager.insertOrUpdate(sysDic);
        ModelMap model = new ModelMap();
        model.addAttribute("state", true);
        return model;
    }

    @RequestMapping(value = "listSysDic.json")
    @ResponseBody
    public ModelMap listSysDic(ModelMap modelMap, @RequestBody SysDic sysDic) {
        Pager pager = new Pager(1, 10);
        QueryResult<SysDic> queryResult = new QueryResult<>();
        Long total = sysDicManager.countSysDic(sysDic);
        queryResult.setTotal(total);
        if (Long.compare(total, 0) > 0) {
            List<SysDic> sysDics = sysDicManager.listSysDic(sysDic, pager);
            queryResult.setRows(sysDics);
        }
        ModelMap model = new ModelMap();
        model.addAttribute("state", true);
        model.addAttribute("message", "操作成功");
        model.addAttribute("data", queryResult);
        return model;

    }




/*@RequestMapping(value = "test")
    @ResponseBody
    public String test(String name) {
        return "hello word ! ! ! " + name;
    }
*/

  /*
    @RequestMapping(value = "edit3")
    public ModelAndView  edit3(ModelAndView modelAndView) {
        modelAndView.addObject("name","tom");
        modelAndView.setViewName("pages/sysdicedit.html");
        return modelAndView;
    }
*/
}
